<template>
  <div class="containers">
    <div class="container">
      <div class="handle-box">

        <!-- <el-button type="primary" icon="el-icon-plus" @click="addregion" class="mr10" size="small">添加</el-button> -->
        <el-select v-model="level" @change="getDataPage" placeholder="会员级别" size="mini" class="mr10" clearable style="width: 150px;">
          <el-option v-for="(levelName, levelKey) in levellist"  :key="levelKey" :label="levelName" :value="parseInt(levelKey)">
          </el-option>
        </el-select>
        <el-select v-model="new_level" @change="getDataPage" placeholder="星级会员" size="mini" class="mr10" clearable style="width: 150px;">
          <el-option v-for="(item, index) in newLive"  :key="index" :label="item.name" :value="parseInt(index)">
          </el-option>
        </el-select>

        <el-select v-model="department" @change="getDataPage" placeholder="所属部门" size="mini" class="mr10" clearable style="width: 150px;">
          <el-option v-for="(item, index) in departmentList"  :key="index" :label="item.name" :value="item.user_id">
          </el-option>
        </el-select>

        <el-select v-model="frozen" @change="getDataPage" placeholder="账号状态" size="mini" class="mr10" clearable style="width: 150px;">
          <el-option v-for="(item, index) in frozenList"  :key="index" :label="item.name" :value="parseInt(index)">
          </el-option>
        </el-select>

        <el-select v-model="activate" @change="getDataPage" placeholder="是否激活会员" size="mini" class="mr10" clearable style="width: 150px;">
          <el-option v-for="(item, index) in activateList"  :key="index" :label="item.name" :value="parseInt(index)">
          </el-option>
        </el-select>

        <el-select v-model="sortTime" @change="changeSort"  size="mini" class="mr10" style="width: 150px;">
          <el-option v-for="(item, index) in sortList"  :key="index" :label="item.name" :value="parseInt(index)">
          </el-option>
        </el-select>


        <el-input v-model="keyword" placeholder="关键词" class="handle-input mr10" size="mini" style="width: 150px;"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"  size="mini">搜索</el-button>
        <el-button type="primary" @click="exportExcel" size="mini" class="mr10" plain>导出</el-button>
        <!-- <el-button type="primary" @click="importGreen(1)" class="mr10"  size="mini">导入绿色积分</el-button>
        <el-button type="primary" @click="importGreen(2)" class="mr10"  size="mini">导入资产积分</el-button> -->
        <el-dropdown size="mini" split-button type="primary" @command="leadingIn">
          导入
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="1">导入绿色积分</el-dropdown-item>
            <el-dropdown-item command="2">导入资产积分</el-dropdown-item>
            <el-dropdown-item command="3">导入资产红包</el-dropdown-item>
            <el-dropdown-item command="4">导入补贴红包</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <el-table :data="tableData" class="table" ref="multipleTable" header-cell-class-name="table-header" border size="mini" :height="$tableHight"
        @selection-change="handleSelectionChange" v-loading="loading" id="out-table" style="width: 100%" @sort-change="handleSortChange">
        <el-table-column type="index" label="序号" width="50" fixed align="center"></el-table-column>
        <el-table-column prop="user_id" label="分享码" width="140" fixed></el-table-column>
        <el-table-column prop="nickname" label="用户昵称" show-overflow-tooltip width="100" fixed></el-table-column>
        <el-table-column prop="mobile" label="用户电话" width="120" fixed></el-table-column>
        <el-table-column prop="department_name" label="所属部门" width="120" show-overflow-tooltip></el-table-column>



        <el-table-column prop="level" label="是否激活" width="100" >
          <template slot-scope="scope">
            <div>{{ scope.row.activate == 1 ? '是' : '否'   }}</div>
          </template>
        </el-table-column>

        <el-table-column prop="level" label="会员级别" width="100" >
          <template slot-scope="scope">
            <div>{{ levellist[scope.row.level] }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="new_level" label="星级会员" width="100" >
          <template slot-scope="scope">
            <div>{{ getLevelName(scope.row.new_level) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="cloud" label="云仓积分" show-overflow-tooltip width="110" sortable="custom"></el-table-column>
        <el-table-column prop="asset_red" label="资产红包" show-overflow-tooltip width="110" sortable="custom"></el-table-column>
        <el-table-column prop="subsidy_red" label="补贴红包" show-overflow-tooltip width="110" sortable="custom"></el-table-column>
        <el-table-column prop="money" label="消费券" show-overflow-tooltip width="90" sortable="custom"></el-table-column>
        <el-table-column prop="coupon" label="补贴卷" show-overflow-tooltip width="90" sortable="custom"></el-table-column>
        <el-table-column prop="asset" label="资产积分" show-overflow-tooltip width="110" sortable="custom"></el-table-column>
        <el-table-column prop="green" label="绿色积分" show-overflow-tooltip width="110" sortable="custom"></el-table-column>
        <el-table-column prop="rights" label="权益值" show-overflow-tooltip width="90" sortable="custom"></el-table-column>
        <el-table-column prop="contribution" label="贡献值" show-overflow-tooltip sortable="custom" width="90"></el-table-column>
        <!-- <el-table-column prop="prove" label="贡献值凭证" sortable></el-table-column> -->


        <el-table-column label="账号状态" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.frozen ? 'danger' : 'success'" effect="dark" size="mini"> {{ scope.row.frozen ? '冻结' : '未冻结'
              }}</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="身份" align="center">
          <template slot-scope="scope">

            <el-tag   effect="dark" size="mini"> {{ scope.row.customer==1?'客服':'用户' }}</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column prop="parent.nickname" label="推荐人" show-overflow-tooltip width="90"></el-table-column>
        <el-table-column prop="parent.mobile" label="推荐人电话" width="120"></el-table-column>

        <el-table-column label="注册时间" align="center" width="160">
          <template slot-scope="scope">
            {{ $clearTimer(scope.row.CreatedAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="340" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)" size="mini">编辑</el-button>
            <el-button type="text" class="red" @click="handleDelete(scope.$index, scope.row)" size="mini">删除</el-button>

            <el-button type="text" @click="handleEdits(scope.row)" size="mini">交易记录</el-button>
            <el-button type="text" @click="modify_rec(scope.row)" size="mini">修改推荐人</el-button>
            <el-button type="text" @click="openRedPacketConvert(scope.row)" size="mini">红包转化</el-button>
            <el-button type="text" @click="deleteRealName(scope.row)" v-if="scope.row.username" size="mini">删除实名</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handlePageChange"
          :current-page="query.pageIndex" :page-sizes="[20, 30, 40, 50]" :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" size="mini">
        </el-pagination>
      </div>
    </div>
    <!-- 编辑 -->
    <el-dialog :visible.sync="editVisible" width="50%">

      <el-descriptions class="margin-top" title="个人信息"  :column="4" size="mini" border>

        <el-descriptions-item label="用户昵称">{{ rowData.nickname }}</el-descriptions-item>
        <el-descriptions-item label="用户电话">{{ rowData.mobile }}</el-descriptions-item>

        <el-descriptions-item label="消费券"> {{ rowData.money }}
        </el-descriptions-item>
        <el-descriptions-item label="补贴卷">{{ rowData.coupon }}</el-descriptions-item>
        <el-descriptions-item label="资产积分">{{ rowData.asset }}</el-descriptions-item>
        <el-descriptions-item label="绿色积分">{{ rowData.green }}</el-descriptions-item>
        <el-descriptions-item label="权益值">{{ rowData.rights }}</el-descriptions-item>
        <el-descriptions-item label="贡献值">{{ rowData.contribution }}</el-descriptions-item>
      </el-descriptions>

      <el-form ref="updata" :model="updata" label-width="100px" style="padding-top: 20px;">
        <el-row  :gutter="24">
          <el-col :span="8">
            <el-form-item label="消费卷">
              <el-input v-model="updata.money" size="mini"></el-input>
              <el-input v-model="updata.money_notes" placeholder="请输入修改消费卷备注" size="mini"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="补贴卷">
              <el-input v-model="updata.coupon" size="mini"></el-input>
              <el-input v-model="updata.coupon_notes" placeholder="请输入修改补贴卷备注" size="mini"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="云仓积分">
              <el-input v-model="updata.cloud" size="mini"></el-input>
              <el-input v-model="updata.cloud_notes" placeholder="请输入修改云仓积分备注" size="mini"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="资产积分">
              <el-input v-model="updata.asset" size="mini"></el-input>
              <el-input v-model="updata.asset_notes" placeholder="请输入修改资产积分备注" size="mini"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="登陆密码">
              <el-input v-model="updata.password"  size="mini" ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="支付密码">
              <el-input v-model="updata.pay_password"  size="mini" ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="手机号">
              <el-input v-model="updata.mobile" size="mini"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="代理比例">
              <el-input v-model="updata.agent" size="mini"></el-input>
            </el-form-item>
          </el-col>


          <el-col :span="8">
            <el-form-item label="会员级别">
              <el-select v-model="updata.level" placeholder="请选择会员级别" size="mini" style="width:100%">
                <el-option
                  v-for="(levelName, levelKey) in levellist"
                  :key="levelKey"
                  :label="levelName"
                  :value="parseInt(levelKey)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="星级会员">
              <el-select v-model="updata.new_level" placeholder="请选择星级会员" size="mini" style="width:100%">
                <el-option
                  v-for="(it) in newLive"
                  :key="it.value"
                  :label="it.name"
                  :value="parseInt(it.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="8">
            <el-form-item label="登录冻结">
              <el-switch v-model="updata.frozen" active-color="#409eff" inactive-color="#f1f1f1"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否激活">
              <el-switch v-model="updata.activate" active-color="#409eff" inactive-color="#f1f1f1" :active-value="1"  :inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="消费卷转账">
              <el-switch v-model="updata.is_transfer" active-color="#409eff" inactive-color="#f1f1f1" :active-value="1"  :inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="补贴红包转账">
              <el-switch v-model="updata.is_transfer_subsidy_red" active-color="#409eff" inactive-color="#f1f1f1" :active-value="1"  :inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="补贴卷转账">
              <el-switch v-model="updata.is_transfer_coupon" active-color="#409eff" inactive-color="#f1f1f1" :active-value="1"  :inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="mini">取 消</el-button>
        <el-button type="primary" @click="saveEdit" size="mini">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加会员 -->
    <el-dialog title="添加" :visible.sync="editVisibles" width="40%">
      <el-form ref="form" :model="form" label-width="70px" style="padding-top: 20px;">
        <el-form-item label="会员电话"><el-input v-model="form.mobile" size="mini"></el-input></el-form-item>
        <el-form-item label="分享码"><el-input v-model="form.share" size="mini"></el-input></el-form-item>
        <el-form-item label="登录密码"><el-input v-model="form.password" size="mini"></el-input></el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="mini">取 消</el-button>
        <el-button type="primary" @click="saveEdits" size="mini">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改推荐人 -->
    <el-dialog title="选择推荐人" :visible.sync="showbox_parent" width="50%" @close='closeDialog'>
      <el-input v-model="keywords" placeholder="编号/电话" class="handle-input mr10" size="mini"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="handleSearch(1)" size="mini">搜索</el-button>
      <el-button type="primary" icon="el-icon-close" @click="showbox_parent = !showbox_parent" size="mini">退出</el-button>
      <el-form ref="form" :model="form" label-width="100px">
        <el-table :data="tableDatas" border class="table" ref="multipleTable" header-cell-class-name="table-header"
          @selection-change="handleSelectionChange" @row-click="getrows" size="mini">
          <el-table-column label="选择" width="80" fixed>
            <template slot-scope="scope">
              <el-radio class="radio" v-model="templateSelection_ids" :label="scope.row.ID">&nbsp;</el-radio>
            </template>
          </el-table-column>
          <el-table-column prop="ID" label="ID" align="center" width="80"></el-table-column>
          <el-table-column prop="nickname" label="姓名" align="center"></el-table-column>
          <el-table-column prop="mobile" label="电话" align="center" min-width="100"></el-table-column>
          <el-table-column align="center" label="状态">
            <template slot-scope="scope">
              <el-button type="primary" @click="btnuser(scope.row.ID)" size="mini">选择此会员</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"
            :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChanges" size="mini"></el-pagination>
        </div>
      </el-form>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="titleDialog" :visible.sync="importVisible" width="30%">
      <el-form ref="importForm" :model="importForm" label-width="80px" style="padding-top: 20px;">
        <el-form-item label="选择文件">
          <el-upload
            class="upload-demo"
            drag
            action=""
            :auto-upload="false"
            :on-change="handleFileChange"
            :file-list="fileList"
            accept=".xls,.xlsx,.csv">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传xls/xlsx/csv文件，且不超过10MB </div>

          </el-upload>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="downloadTemplate" type="primary" plain size="mini">下载模版</el-button>
        <el-button @click="importVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitImport" size="mini" :loading="importLoading">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 红包转化对话框 -->
    <el-dialog title="红包转化" :visible.sync="redPacketConvertVisible" width="30%">
      <el-form ref="redPacketConvertForm" :model="redPacketConvertForm" :rules="redPacketConvertRules" label-width="80px">
        <el-form-item label="转化金额" prop="money">
          <el-input v-model="redPacketConvertForm.money" size="mini" type="number"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="redPacketConvertForm.notes" size="mini"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="redPacketConvertVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitRedPacketConvert" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
  name: 'basetable',
  data() {
    return {
      query: {
        pageIndex: 1,
        pageSize: 20
      },
      new_level:'',
      state: 0,
      upload: '',
      editVisible: false,
      editVisibles: false,
      titleDialog:"导入绿色积分",
      importVisible: false, // 导入对话框显示控制
      importLoading: false, // 导入按钮加载状态
      tableData: [],
      tableDatas: [],
      pageTotal: 0,
      type: 0,
      levellist: [],
      keyword: '',//关键词
      keywords: '',
      updata: {},//修改
      form: {
        mobile: '',
        password: '',
        share: '',
      },//添加
      importForm: {
        file: null
      }, // 导入表单
      fileList: [], // 上传文件列表
      loading: false,
      showbox_parent: false,
      templateSelection_ids: 0,    //修改推荐人选择
      modifYpeople: '',  //修改人ID
      sort: 0,
      sortTime:0,
      level:'',
      rowData:{},
      redPacketConvertVisible: false, // 红包转化弹框显示控制
      redPacketConvertForm: { // 红包转化表单数据
        id: '',
        money: '',
        notes: ''
      },
      redPacketConvertRules: { // 红包转化表单验证规则
        money: [{ required: true, message: '请输入转化金额', trigger: 'blur' }],
        notes: [{ required: true, message: '请输入备注', trigger: 'blur' }]
      },
      newLive:[
        {name:'普通',value:0,},
        {name:'1星', value:1,},
        {name:'2星', value:2,},
        {name:'3星', value:3,},
        {name:'4星', value:4,},
        {name:'5星', value:5,},
        {name:'6星', value:6,},
        {name:'7星', value:7,},
      ],
      frozenList:[
        {name:'未冻结', value:0,},
        {name:'冻结', value:1,},
      ],
      frozen:'',
      activateList:[
        {name:'非激活会员', value:0,},
        {name:'激活会员', value:1,},
      ],
      activate:'',
      sortList:[
        {name:'按时间升序', value:0,},
        {name:'按时将降序', value:1,},
      ],
      departmentList:[],
      department:''
    };
  },
  created() {
    this.load()
    this.getDepartmentList()

  },
  methods: {
    // 打开红包转化弹窗
    openRedPacketConvert(row) {
      this.redPacketConvertForm.id = row.ID;
      this.redPacketConvertForm.money = '';
      this.redPacketConvertForm.notes = '';
      this.redPacketConvertVisible = true;
    },

    // 提交红包转化
    submitRedPacketConvert() {
      this.$refs.redPacketConvertForm.validate((valid) => {
        if (valid) {
          // 调用红包转化接口
          this.request('admin/assetRedToSubsidyRed/', 'post', {
            id: this.redPacketConvertForm.id,
            money: Number(this.redPacketConvertForm.money),
            notes: this.redPacketConvertForm.notes
          }).then(res => {
            if (res.errno === 0) {
              this.$message.success(res.errmsg || '转化成功');
              this.redPacketConvertVisible = false;
              this.getData(); // 重新加载数据
            }
          }).catch(err => {
            this.$message.error('转化失败: ' + (err.message || '未知错误'));
          });
        }
      });
    },

    getDepartmentList() {
      this.request('admin/departmentList/', 'get', { page: 1, size: 20}).then(res => {
        this.departmentList=res.data.data


      })
    },
    downloadTemplate() {
      // 创建只包含所需字段的模板数据
      const templateData = [
        { "mobile": "15928046651", "money": "100" }
      ];

      // 创建工作表
      const ws = XLSX.utils.json_to_sheet(templateData);

      // 创建工作簿
      const wb = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

      // 写入文件
      const wbout = XLSX.write(wb, {
          bookType: "xlsx",
          bookSST: true,
          type: "array"
      });

      try {
          FileSaver.saveAs(
              new Blob([wbout], { type: "application/octet-stream;charset=utf-8" }),
              "导入绿色积分.xlsx"
          );
      } catch (e) {
          if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },

    getLevelName(value){
      return this.newLive.find(item=>item.value==value).name
    },
    exportExcel() {//导出
      var xlsxParam = { raw: true };//转换成excel时，使用原始的格式
      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"), xlsxParam);
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream;charset=utf-8" }),
          "用户.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },

    // 导入功能
    importGreen(type) {
      if(type == 1) {
        this.titleDialog="导入绿色积分"
      }else if(type == 2) {
        this.titleDialog="导入资产积分"
      }else if(type == 3) {
        this.titleDialog="导入资产红包"
      }else if(type == 4) {
        this.titleDialog="导入补贴红包"
      }
      this.importVisible = true;
      this.fileList = [];
      this.importForm.file = null;
    },

    // 处理文件选择
    handleFileChange(file, fileList) {
      this.fileList = fileList;
      this.importForm.file = file.raw;
    },

    // 提交导入
    submitImport() {
      if (!this.importForm.file) {
        this.$message.warning('请选择要导入的文件');
        return;
      }

      const formData = new FormData();
      formData.append('file', this.importForm.file);

      // 根据titleDialog确定请求的接口
      let requestUrl = '';
      if (this.titleDialog === '导入绿色积分') {
        requestUrl = 'admin/importGreen/';
      } else if (this.titleDialog === '导入资产积分') {
        requestUrl = 'admin/importAsset/';
      } else if (this.titleDialog === '导入资产红包') {
        requestUrl = 'admin/importAssetRed/';
      } else if (this.titleDialog === '导入补贴红包') {
        requestUrl = 'admin/importSubsidyRed/';
      }

      this.importLoading = true;
      this.request(requestUrl, 'post', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        this.importLoading = false;
        if (res.errno === 0) {
          this.importVisible = false;
          this.$confirm(res.errmsg + '是否确认导入？', '提示', {
            confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.importGreenConfirm()
            }).catch(() => {

          });

        } else {
          this.$message.error(res.errmsg || '导入失败');
        }
      }).catch(err => {
        console.log(err)
        this.importLoading = false;
        this.$message.error('导入失败: ' + (err.message || '未知错误'));
      });
    },
    importGreenConfirm() {
      // 根据titleDialog确定请求的接口
      let requestUrl = '';
      if (this.titleDialog === '导入绿色积分') {
        requestUrl = 'admin/importGreen/';
      } else if (this.titleDialog === '导入资产积分') {
        requestUrl = 'admin/importAsset/';
      } else if (this.titleDialog === '导入资产红包') {
        requestUrl = 'admin/importAssetRed/';
      } else if (this.titleDialog === '导入补贴红包') {
        requestUrl = 'admin/importSubsidyRed/';
      }

      this.request(requestUrl, 'put').then(
        res => {
          if (res.errno == 0) {
            this.$message.success(res.errmsg);
            this.getData(this.keyword)
          }
        },
        function () {
          console.log('请求失败处理');
        }
      );
    },
    leadingIn(e) {
      console.log(e)
      this.importGreen(e)
    },
    // 交易记录
    handleEdits(e) {
      this.$router.push({ path: '/finance/wallet', query: { mobile: e.mobile } });
    },
    getDataPage() {
      this.query.pageIndex = 1
      this.getData();
    },
    load() {//获取级别
      this.request('admin/setLevel/', 'get', {}).then(
        res => {
          if (res.errno == 0) {
            for (let i in res.data.level) {
              this.levellist[i] = res.data.level[i]
            }
            console.log(this.levellist)

          }
          this.getData()
        },
        function () {
          console.log('请求失败处理');
        }
      );
    },
    //设置客服
    setkefu(id) {//获取级别
      this.request('admin/userInfo/', 'put', { id: id }).then(
        res => {
          if (res.errno == 0) {
            this.$message.success(res.errmsg);
            this.getData()
          } else {
            this.$message.error(res.errmsg);
          }
        },
        function () {
          console.log('请求失败处理');
        }
      );
    },
    //修改size值
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.query.pageSize = val
      this.getData();
    },
    changeSort(data){
      this.sort = data
      this.getData()
    },
    //获取数据
    getData(data, i) {
      this.loading = true
      let formData = {
        sort: this.sort < 3 ? this.sortTime : this.sort,
        page: this.query.pageIndex,
        size: this.query.pageSize,
        keyword: data,
        level: this.level,
        new_level:this.new_level,
        frozen:this.frozen,
        activate:this.activate,
        department:this.department,
      }
      this.request('admin/userInfo/', 'get',formData).then(res => {
        if (i == 1) {
          this.tableDatas = res.data.data
        } else {
          this.tableData = res.data.data
          console.log(res.data.data)
        }
        this.pageTotal = res.data.total
      })
      this.loading = false
    },
    // 编辑
    handleEdit(e) {
      console.log(e);
      this.rowData = e
      this.updata = {
        new_level:e.new_level,
        money:'', //消费券
        coupon:'', //补贴卷
        asset:'', //资产积分
        money_notes:'', //消费券备注
        coupon_notes:'', //补贴卷备注
        cloud:'',
        cloud_notes:'',
        asset_notes:'', //资产积分备注
        pay_password:'', //支付密码
        mobile:'',//手机号
        agent:e.agent, //代理比例
        level:e.level,//会员级别
        frozen:e.frozen,//登录冻结
        password:'', //登录密码
        id:e.ID,
        activate:e.activate,
        is_transfer:e.is_transfer, //是否转账
        is_transfer_subsidy_red:e.is_transfer_subsidy_red,
        is_transfer_coupon:e.is_transfer_coupon
      }
      this.editVisible = true
    },
    //修改用户信息
    saveEdit() {
      this.updata.money = Number(this.updata.money)
      this.updata.coupon = Number(this.updata.coupon)
      this.updata.asset = Number(this.updata.asset)
      this.updata.agent = Number(this.updata.agent)
      this.updata.activate = Number(this.updata.activate)
      this.updata.cloud = Number(this.updata.cloud)
      this.request('admin/userInfo/', 'post', this.updata).then(res => {
        if (res.errno == 0) {
          this.$message.success(res.errmsg);
          this.getData(this.keyword)
        } else {
          this.$message.error(res.errmsg);
        }
        this.editVisible = false

      })
    },
    //添加用户
    saveEdits() {
      this.request('admin/adminRegister/', 'post', this.form).then(res => {
        if (res.errno == 0) {
          this.$message.success(res.errmsg);
          this.getData()
        } else {
          this.$message.error(res.errmsg);
        }
        this.editVisibles = false
        this.getData()
      })
    },
    cancel() {
      this.editVisible = false
      this.editVisibles = false
    },

    handleSortChange(e) {
      if(e.order == 'ascending' && e.prop == 'money') {
        this.sort = 2
      }
      if(e.order == 'descending' && e.prop == 'money') {
        this.sort = 3
      }

      if(e.order == 'ascending' && e.prop == 'coupon') {
        this.sort = 4
      }

      if(e.order == 'descending' && e.prop == 'coupon') {
        this.sort = 5
      }
      if(e.order == 'ascending' && e.prop == 'asset') {
        this.sort = 6
      }

      if(e.order == 'descending' && e.prop == 'asset') {
        this.sort = 7
      }

      if(e.order == 'ascending' && e.prop == 'green') {
        this.sort = 8
      }
      if(e.order == 'descending' && e.prop == 'green') {
        this.sort = 9
      }

      this.getData()
    },

    //点击修改推荐联系人
    modify_rec(e) {
      this.modifYpeople = e.ID
      this.showbox_parent = true
      this.getData(null, 1);
    },
    getrows(row) {
      this.templateSelection_ids = parseInt(row.ID)
    },
    //选择会员
    btnuser(e) {
      this.request('admin/modifyShare/', 'POST', { id: this.modifYpeople, share: this.templateSelection_ids }).then(res => {
        if (res.errno === 0) {
          this.showbox_parent = false
          this.$message.success(res.errmsg);
          this.getData()
        } else {
          this.$message.error(res.errmsg)
        }
      })
    },
    // 触发搜索按钮
    handleSearch(i) {
      let n = ''
      if (i == 1) {
        n = this.keywords
      } else {
        n = this.keyword
      }
      this.$set(this.query, 'pageIndex', 1);
      this.getData(n, i);
    },
    handlePageChanges(val) {
      this.$set(this, 'userpage', val);
      this.userpage = val
      this.getData(null, 1);
    },
    //关闭联系人推荐
    closeDialog() {
      this.keywords = ''
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除电话为' + row.mobile + '的用户吗？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.request('admin/userInfo/', 'delete', { ID: row.ID }).then(res => {
            if (res.errno == 0) {
              this.$message.success(res.errmsg);
              this.tableData.splice(index, 1)
              this.getData()
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
    },
    deleteRealName(row) {
      this.$confirm('确定要删除该有用户的实名认证吗？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.request('admin/deleteRealName/', 'post', { ID: row.ID }).then(res => {
            if (res.errno == 0) {
              this.$message.success(res.errmsg);
              this.getData()
            }
          });
        })
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 添加
    addregion() {
      this.editVisibles = true
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val);
      this.getData();
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
}

.el-upload--text {
  width: 200px !important;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;

  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  /* width: 100px; */
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100%;
  height: 178px;
  display: block;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 60px;
  height: 60px;

  .el-image img {
    object-fit: cover;
  }
}

/* 设置分页按钮样式 */
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
/* 使用 /deep/ 或 >>> 来穿透 scoped 样式 */
::v-deep .el-table th.el-table__cell {
  background-color: #f2f2f2;
  /* 设置表头颜色 */
  color: #333;
  /* 设置表头文字颜色 */
}

</style>
